package com.neuedu.service.impl;
import com.neuedu.dao.AddChongwuNumberDao;
import com.neuedu.dao.ChongWuDao;
import com.neuedu.dao.impl.AddChongwuNumberDaoImpl;
import com.neuedu.dao.impl.ChongWuDaoImpl;
import com.neuedu.model.AddChongwuNumber;
import com.neuedu.model.ChongWu;
import com.neuedu.service.ChongWuService;
import com.neuedu.vo.PageVO;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 徐丹
 * @date 2024/9/12
 */
public class ChongWuServiceImpl implements ChongWuService {

    private ChongWuDao chongWuDao=new ChongWuDaoImpl();

    private AddChongwuNumberDao addChongwuNumberDao=new AddChongwuNumberDaoImpl();

    @Override
    public PageVO<ChongWu> selectPage(String minDate, String maxDate, String likeName, Integer pageNo, Integer pageSize) {
        Integer limitParams1=(pageNo-1)*pageSize;
        Integer limitParams2=pageSize;
        List<ChongWu> chongWus = chongWuDao.selectPage(minDate, maxDate, likeName, limitParams1, limitParams2);
        PageVO<ChongWu> pageVO=new PageVO<>();
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        pageVO.setData(chongWus);
        //还差  pageCount页的总数  itemCount数据的总数
        Integer itemCount=chongWuDao.count(minDate, maxDate, likeName);
        Integer pageCount=((itemCount%pageSize)==0)?(itemCount/pageSize):(itemCount/pageSize+1);
        pageVO.setItemCount(itemCount);
        pageVO.setPageCount(pageCount);
        return pageVO;
    }

    @Override
    public int addChongWu(ChongWu chongWu) {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);

        AddChongwuNumber  addChongwuNumber = addChongwuNumberDao.select();
        String number="XY";
        number=number+format.replace("-","");
        if(addChongwuNumber.getNumberTime().equals(format)){
            if(addChongwuNumber.getNumber()<10){
                number=number+"00"+addChongwuNumber.getNumber();
            }else if(addChongwuNumber.getNumber()<100){
                number=number+"0"+addChongwuNumber.getNumber();
            }else{
                number=number+addChongwuNumber.getNumber();
            }
            addChongwuNumber.setNumber(addChongwuNumber.getNumber()+1);
            addChongwuNumberDao.update(addChongwuNumber);
        }else{
            addChongwuNumber.setNumberTime(format);
            addChongwuNumber.setNumber(1);
            addChongwuNumberDao.update(addChongwuNumber);
            number=number+"001";
        }
        //处理创建日期
        sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format = sdf.format(date);
        chongWu.setChuangjianshijian(format);
        return chongWuDao.insertChongWu(chongWu);
    }

    @Override
    public int delChongWu(Integer[] ids) {
        return chongWuDao.deleteChongWu(ids);
    }

    @Override
    public int saveChongWu(ChongWu chongWu) {
        return chongWuDao.updateChongWu(chongWu);
    }

    @Override
    public ChongWu queryChongWu(Integer id) {
        return chongWuDao.selectChongWuById(id);
    }
}
